package com.ruoyi.postulant.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.postulant.domain.Apply;
import com.ruoyi.postulant.domain.Message;
import com.ruoyi.postulant.service.IApplyService;
import com.ruoyi.postulant.service.IMessageService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.postulant.domain.Activity;
import com.ruoyi.postulant.service.IActivityService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 活动Controller
 *
 * @author wanghong
 * @date 2025-03-15
 */
@RestController
@RequestMapping("/postulant/activity")
public class ActivityController extends BaseController
{
    @Autowired
    private IActivityService activityService;

    @Autowired
    private IApplyService applyService;

    @Autowired
    private IMessageService messageService;

    /**
     * 查询活动列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Activity activity)
    {
        startPage();
        List<Activity> list = activityService.selectActivityList(activity);
        return getDataTable(list);
    }

    /**
     * 查询活动列表
     */
    @GetMapping("/getActivityList")
    public TableDataInfo getActivityList(Activity activity)
    {
        Long userId = SecurityUtils.getUserId();
        activity.setUserId(Math.toIntExact(userId));
        startPage();
        List<Activity> list = activityService.getActivityList(activity);
        return getDataTable(list);
    }

    /**
     * 查询活动列表
     */
    @GetMapping("/getApplyActivityList")
    public TableDataInfo getApplyActivityList(Activity activity)
    {
        Long userId = SecurityUtils.getUserId();
        activity.setUserId(Math.toIntExact(userId));
        startPage();
        List<Activity> list = activityService.getApplyActivityList(activity);
        return getDataTable(list);
    }



    /**
     * 导出活动列表
     */
    @PreAuthorize("@ss.hasPermi('postulant:activity:export')")
    @Log(title = "活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Activity activity)
    {
        List<Activity> list = activityService.selectActivityList(activity);
        ExcelUtil<Activity> util = new ExcelUtil<Activity>(Activity.class);
        util.exportExcel(response, list, "活动数据");
    }

    /**
     * 获取活动详细信息
     */
    @PreAuthorize("@ss.hasPermi('postulant:activity:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(activityService.selectActivityById(id));
    }

    /**
     * 新增活动
     */
    @PreAuthorize("@ss.hasPermi('postulant:activity:add')")
    @Log(title = "活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Activity activity)
    {
        return toAjax(activityService.insertActivity(activity));
    }

    /**
     * 修改活动
     */
    @PreAuthorize("@ss.hasPermi('postulant:activity:edit')")
    @Log(title = "活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Activity activity)
    {
        Activity a = activityService.getById(activity.getId());
        if(activity.getState() > a.getState()){
            //查询该活动报名记录
            List<Apply> applyList = applyService.list(new QueryWrapper<Apply>().eq("activity_id", activity.getId()));
            List<Message> messageList = new ArrayList<>();
            for (Apply apply : applyList) {
                //到下一步状态发送用户消息
                Message message = new Message();
                message.setActivityId(activity.getId());
                message.setUserId(apply.getUserId());
                message.setTitle("活动最新消息");
                message.setContent("您报名的活动状态["+a.getTitle()+"]状态已经更新，请立即查看已报名活动信息");
                message.setCreateTime(new Date());
                messageList.add(message);
            }
            messageService.saveBatch(messageList);
        }
        return toAjax(activityService.updateActivity(activity));
    }

    /**
     * 删除活动
     */
    @PreAuthorize("@ss.hasPermi('postulant:activity:remove')")
    @Log(title = "活动", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(activityService.deleteActivityByIds(ids));
    }
}
